﻿using System;
using System.Configuration;
using System.Web.Profile;
using Tiscs.Practices.Diagnostics;

namespace Tiscs.Practices.Web.Profile
{
    /// <summary>
    /// 用户配置信息类扩展。
    /// </summary>
    internal static class ProfileBaseExtension
    {
        /// <summary>
        /// 判断配置属性是否支持匿名用户。
        /// </summary>
        /// <param name="profile">用户配置信息类实例。</param>
        /// <param name="propertyName">要判断的配置属性名称。</param>
        /// <returns>指示是否支持匿名用户的布尔值。</returns>
        public static bool IsAllowAnonymous(this ProfileBase profile, string propertyName)
        {
            Exception exception;
            if (!Validation.Parameter<string>(nameof(propertyName)).AssertNotNull().CheckRules(propertyName, out exception)) {
                throw exception;
            }
            SettingsProperty property = ProfileBase.Properties[propertyName];
            if (!Validation.Parameter<SettingsProperty>(nameof(property)).Assert(p => p != null, n => new ArgumentOutOfRangeException(n)).CheckRules(property, out exception))
            {
                throw exception;
            }
            return (bool)property.Attributes["AllowAnonymous"];
        }
    }
}
